//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<IDeviceResource.cpp>
///	@path	~/src/graphics/
///	@date	2007/09/30
///	@desc	Device resource, including vertex buffer, index buffer or texture.

#include "config/config.h"
#include "config/os.h"

#include "graphics/IDeviceResource.h"

namespace xeres {

	// ResourceMap
	ResourceMap::ResourceMap( void )
		: m_pData( NULL )
		, m_totalData( 0 )
		, m_stride( 0 )
		, m_count( 0 )
		, m_row( 0 )
		, m_depth( 0 )
		, m_slice( 0 )
	{
	}
	// ResourceMap
	ResourceMap::ResourceMap( const ResourceMap& rhs )
		: m_pData( rhs.m_pData )
		, m_totalData( rhs.m_totalData )
		, m_stride( rhs.m_stride )
		, m_count( rhs.m_count )
		, m_row( rhs.m_row )
		, m_depth( rhs.m_depth )
		, m_slice( rhs.m_slice )
	{
	}
	// Map
	void ResourceMap::Map( byte * data , size_t total , size_t stride , size_t row , size_t col )
	{
		XS_ASSERT( m_pData == NULL );
		m_pData = data;
		m_totalData = total;
		m_stride = stride;
		m_row = row;
		m_count = col;
	}
	// Map
	void ResourceMap::Map( byte * data , size_t total , size_t stride , size_t row , size_t col , size_t depth , size_t slice )
	{
		XS_ASSERT( m_pData == NULL );
		m_pData = data;
		m_totalData = total;
		m_stride = stride;
		m_row = row;
		m_count = col;
		m_depth = depth;
		m_slice = slice;
	}
	// Map
	void ResourceMap::Map( byte * data , size_t total , size_t stride , size_t count )
	{
		XS_ASSERT( m_pData = data );
		m_pData = data;
		m_totalData = total;
		m_stride = stride;
		m_count = count;
	}
	// Unmap
	void ResourceMap::Unmap( void )
	{
		m_pData = NULL;
		m_totalData = 0;
		m_stride = 0;
		m_count = 0;
		m_row = 0;
		m_depth = 0;
		m_slice = 0;
	}
	// GetPixel
	byte * ResourceMap::GetPixel( uint x , uint y ) const
	{
		XS_ASSERT( x < m_count && y < m_row );
		size_t offset = m_stride * y + x * ( m_stride / m_count );
		XS_ASSERT( offset < m_totalData );
		return m_pData + offset;
	}
	// GetPixel
	byte * ResourceMap::GetPixel( uint x , uint y , uint z ) const
	{
		XS_ASSERT( x < m_count && y < m_row && z < m_depth );
		size_t offset = m_stride * y + x * ( m_stride / m_count ) + m_slice * z;
		XS_ASSERT( offset < m_totalData );
		return m_pData + offset;
	}
	// GetRow
	byte * ResourceMap::GetRow( uint y ) const
	{
		XS_ASSERT( y < m_row );
		size_t offset = m_stride * y;
		XS_ASSERT( offset < m_totalData );
		return m_pData + offset;
	}
	// GetRow
	byte * ResourceMap::GetRow( uint y , uint z ) const
	{
		XS_ASSERT( y < m_row && z < m_depth );
		size_t offset = m_stride * y + m_slice * z;
		XS_ASSERT( offset < m_totalData );
		return m_pData + offset;
	}
	// GetVertex
	byte * ResourceMap::GetVertex( uint index ) const
	{
		XS_ASSERT( index < m_count );
		size_t offset = m_stride * index;
		XS_ASSERT( offset < m_totalData );
		return m_pData + offset;
	}
	// GetIndex
	byte * ResourceMap::GetIndex( uint index ) const
	{
		XS_ASSERT( index < m_count );
		size_t offset = m_stride * index;
		XS_ASSERT( offset < m_totalData );
		return m_pData + offset;
	}

	IMPL_IOBJECT_INTERFACE( IDeviceResource );

	// ctor
	IDeviceResource::IDeviceResource( void )
	{
	}

	// dtor
	IDeviceResource::~IDeviceResource( void )
	{
	}

} // namespace xeres
